home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir42 / gnudbm14.zip / GDBMSEQ.C < prev    next >
C/C++ Source or Header  |  1990-08-24  |  5KB  |  162 lines

  1. /* gdbmseq.c - Routines to visit all keys.  Not in sorted order. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 1, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.         phone:  (206) 676-3035
  27.        
  28. *************************************************************************/
  29.  
  30. /*
  31.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@@ddagsi3.bitnet
  32.  *
  33.  * To this port, the same copying conditions apply as to the
  34.  * original release.
  35.  *
  36.  * IMPORTANT:
  37.  * This file is not identical to the original GNU release!
  38.  * You should have received this code as patch to the official
  39.  * GNU release.
  40.  *
  41.  * MORE IMPORTANT:
  42.  * This port comes with ABSOLUTELY NO WARRANTY.
  43.  *
  44.  * $Header: e:/gnu/gdbm/RCS/gdbmseq.c'v 1.4.0.1 90/08/16 09:22:33 tho Exp $
  45.  */
  46.  
  47. #include <stdio.h>
  48. #include <sys/types.h>
  49. #ifndef MSDOS
  50. #include <sys/file.h>
  51. #endif /* not MSDOS */
  52. #include <sys/stat.h>
  53. #include "gdbmdefs.h"
  54. #include "systems.h"
  55.  
  56. #ifdef __STDC__
  57. static void get_next_key (gdbm_file_info *dbf, int elem_loc, datum *return_val);
  58. #endif /* not __STDC__ */
  59.  
  60. /* Special extern for this file. */
  61. extern char *_gdbm_read_entry ();
  62.  
  63.  
  64. /* Find and read the next entry in the hash structure for DBF starting
  65.    at ELEM_LOC of the current bucket and using RETURN_VAL as the place to
  66.    put the data that is found. */
  67.  
  68. static void
  69. get_next_key (dbf, elem_loc, return_val)
  70.      gdbm_file_info *dbf;
  71.      int elem_loc;
  72.      datum *return_val;
  73. {
  74.   int   found;            /* Have we found the next key. */
  75.   char  *find_data;        /* Data pointer returned by find_key. */
  76.  
  77.   /* Find the next key. */
  78.   found = FALSE;
  79.   while (!found)
  80.     {
  81.       /* Advance to the next location in the bucket. */
  82.       elem_loc++;
  83.       if (elem_loc == dbf->header->bucket_elems)
  84.     {
  85.       /* We have finished the current bucket, get the next bucket.  */
  86.       elem_loc = 0;
  87.  
  88.       /* Find the next bucket.  It is possible several entries in
  89.          the bucket directory point to the same bucket. */
  90.       while (dbf->bucket_dir < dbf->header->dir_size / sizeof (LONG)
  91.          && dbf->cache_entry->ca_adr == dbf->dir[dbf->bucket_dir])
  92.         dbf->bucket_dir++;
  93.  
  94.       /* Check to see if there was a next bucket. */
  95.       if (dbf->bucket_dir < dbf->header->dir_size / sizeof (LONG))
  96.         _gdbm_get_bucket (dbf, dbf->bucket_dir);          
  97.       else
  98.         /* No next key, just return. */
  99.         return ;
  100.     }
  101.       found = dbf->bucket->h_table[elem_loc].hash_value != -1;
  102.     }
  103.   
  104.   /* Found the next key, read it into return_val. */
  105.   find_data = _gdbm_read_entry (dbf, elem_loc);
  106.   return_val->dsize = dbf->bucket->h_table[elem_loc].key_size;
  107.   return_val->dptr = (char *) malloc (return_val->dsize);
  108.   if (return_val->dptr == NULL) _gdbm_fatal (dbf, "malloc error");
  109.   bcopy (find_data, return_val->dptr, return_val->dsize);
  110. }
  111.  
  112.  
  113. /* Start the visit of all keys in the database.  This produces something in
  114.    hash order, not in any sorted order.  */
  115.  
  116. datum
  117. gdbm_firstkey (dbf)
  118.      gdbm_file_info *dbf;
  119. {
  120.   datum return_val;        /* To return the first key. */
  121.  
  122.   /* Set the default return value for not finding a first entry. */
  123.   return_val.dptr = NULL;
  124.  
  125.   /* Get the first bucket.  */
  126.   _gdbm_get_bucket (dbf, 0);
  127.  
  128.   /* Look for first entry. */
  129.   get_next_key (dbf, -1, &return_val);
  130.  
  131.   return return_val;
  132. }
  133.  
  134.  
  135. /* Continue visiting all keys.  The next key following KEY is returned. */
  136.  
  137. datum
  138. gdbm_nextkey (dbf, key)
  139.      gdbm_file_info *dbf;
  140.      datum key;
  141. {
  142.   datum  return_val;        /* The return value. */
  143.   int    elem_loc;        /* The location in the bucket. */
  144.   char  *find_data;        /* Data pointer returned by _gdbm_findkey. */
  145.   LONG     hash_val;        /* Returned by _gdbm_findkey. */
  146.  
  147.   /* Set the default return value for no next entry. */
  148.   return_val.dptr = NULL;
  149.  
  150.   /* Do we have a valid key? */
  151.   if (key.dptr == NULL) return return_val;
  152.  
  153.   /* Find the key.  */
  154.   elem_loc = _gdbm_findkey (dbf, key, &find_data, &hash_val);
  155.   if (elem_loc == -1) return return_val;
  156.  
  157.   /* Find the next key. */  
  158.   get_next_key (dbf, elem_loc, &return_val);
  159.  
  160.   return return_val;
  161. }
  162.